<template>
  <div class="login-container">
    <!-- 动态粒子背景 -->
    <div class="particles-container">
      <div 
        v-for="(particle, index) in particles" 
        :key="index"
        class="particle"
        :style="particle.style"
      ></div>
    </div>
    
    <!-- 浮动元素 -->
    <div class="floating-elements">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
    </div>
    
    <div class="login-box">
      <div class="login-header">
        <div class="logo-container">
          <el-icon size="50" class="login-icon neon-icon"><School /></el-icon>
          <div class="logo-glow"></div>
        </div>
        <h2 class="login-title">栗子云</h2>
        <p class="login-subtitle">请登录您的账号</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        @submit.prevent="handleLogin"
      >
        <el-form-item prop="useraccount">
          <el-input
            v-model="loginForm.useraccount"
            placeholder="请输入账号"
            prefix-icon="User"
            size="large"
            class="neon-input"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            class="neon-input"
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button neon-button"
            :loading="loading"
            @click="handleLogin"
          >
            <span class="button-text">登录</span>
            <div class="button-glow"></div>
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button
            size="large"
            class="register-button neon-button-secondary"
            @click="goToRegister"
          >
            <span class="button-text">注册新账号</span>
            <div class="button-glow"></div>
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <p class="demo-account">测试账号：admin001 / dddddddd</p>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { login } from '@/api/auth'
import { useAuthStore } from '@/store/auth'
import bcrypt from 'bcryptjs'

let particleInterval = null

// 初始化粒子动画
const initParticles = () => {
  const particles = []
  const particleCount = 50
  
  for (let i = 0; i < particleCount; i++) {
    particles.push({
      style: {
        left: Math.random() * 100 + '%',
        top: Math.random() * 100 + '%',
        width: Math.random() * 10 + 2 + 'px',
        height: Math.random() * 10 + 2 + 'px',
        animationDelay: Math.random() * 5 + 's',
        opacity: Math.random() * 0.5 + 0.3
      }
    })
  }
  
  return particles
}

export default {
  name: 'Login',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()
    
    // 登录表单数据
    const loginForm = reactive({
      useraccount: '',
      password: ''
    })
    
    // 登录表单验证规则
    const loginRules = {
      useraccount: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
      ]
    }
    
    const loginFormRef = ref()
    const loading = ref(false)
    const particles = ref(initParticles())
    
    // 处理登录
    const handleLogin = async () => {
      if (!loginFormRef.value) return
      
      await loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            // 调用登录API，发送原始密码
            const response = await login({
              useraccount: loginForm.useraccount,
              password: loginForm.password
            })
            
            if (response.success) {
              // 登录成功，保存token和用户信息
              authStore.setToken(response.data.token)
              authStore.setUserInfo(response.data.userInfo)
              
              // 同时保存username到localStorage（为了向后兼容）
              if (response.data.userInfo && response.data.userInfo.username) {
                localStorage.setItem('username', response.data.userInfo.username)
              }
              
              ElMessage.success('登录成功')
              // 跳转到首页
              await router.push('/')
            } else {
              ElMessage.error(response.message || '登录失败')
            }
          } catch (error) {
            console.error('登录请求失败:', error)
            ElMessage.error('登录请求失败')
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    const goToRegister = () => {
      router.push('/register')
    }
    
    onMounted(() => {
      particleInterval = setInterval(() => {
        particles.value = initParticles()
      }, 10000)
    })
    
    onUnmounted(() => {
      if (particleInterval) {
        clearInterval(particleInterval)
      }
    })
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin,
      goToRegister,
      particles
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background: linear-gradient(-45deg, #667eea, #764ba2, #f093fb, #f5576c);
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

/* 注册按钮样式 */
.register-button {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  transition: all 0.3s ease;
}

.register-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.neon-button-secondary {
  position: relative;
  overflow: hidden;
}

.neon-button-secondary .button-glow {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.neon-button-secondary:hover .button-glow {
  left: 100%;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 粒子动画 */
.particles-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.particle {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  animation: float 3s ease-in-out infinite;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); opacity: 1; }
  50% { transform: translateY(-20px) rotate(180deg); opacity: 0.5; }
}

/* 浮动元素 */
.floating-elements {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  animation: floatCircle 20s infinite linear;
}

.circle-1 {
  width: 100px;
  height: 100px;
  top: 10%;
  left: 10%;
  animation-duration: 25s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-duration: 30s;
  animation-direction: reverse;
}

.circle-3 {
  width: 80px;
  height: 80px;
  bottom: 20%;
  left: 70%;
  animation-duration: 20s;
}

@keyframes floatCircle {
  0% { transform: rotate(0deg) translateX(50px) rotate(0deg); }
  100% { transform: rotate(360deg) translateX(50px) rotate(-360deg); }
}

/* 登录框 - 玻璃拟态效果 */
.login-box {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(20px);
  border-radius: 25px;
  padding: 50px 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: 
    0 8px 32px rgba(0, 0, 0, 0.3),
    inset 0 1px 0 rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
  animation: slideIn 0.8s ease-out;
  transform: translateY(0);
}

@keyframes slideIn {
  0% { 
    opacity: 0;
    transform: translateY(-50px) scale(0.9);
  }
  100% { 
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
  animation: fadeInDown 1s ease-out 0.3s both;
}

@keyframes fadeInDown {
  0% {
    opacity: 0;
    transform: translateY(-30px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

.logo-container {
  position: relative;
  display: inline-block;
  margin-bottom: 20px;
}

.neon-icon {
  color: #64b5f6;
  animation: neonGlow 2s ease-in-out infinite alternate;
}

@keyframes neonGlow {
  0% { 
    color: #64b5f6;
    text-shadow: 0 0 10px rgba(100, 181, 246, 0.8);
  }
  100% { 
    color: #42a5f5;
    text-shadow: 
      0 0 20px rgba(100, 181, 246, 1),
      0 0 30px rgba(100, 181, 246, 0.8),
      0 0 40px rgba(100, 181, 246, 0.6);
  }
}

.logo-glow {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, rgba(100, 181, 246, 0.3) 0%, transparent 70%);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  animation: pulseGlow 3s ease-in-out infinite;
}

@keyframes pulseGlow {
  0%, 100% { opacity: 0.3; transform: translate(-50%, -50%) scale(1); }
  50% { opacity: 0.6; transform: translate(-50%, -50%) scale(1.2); }
}

.login-title {
  margin: 0 0 10px 0;
  color: #ffffff;
  font-size: 32px;
  font-weight: 700;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
}

.login-subtitle {
  margin: 0;
  color: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  font-weight: 300;
}

.login-form {
  margin-bottom: 30px;
}

/* 霓虹输入框 */
.neon-input {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.3) !important;
  border-radius: 12px !important;
  color: white !important;
  transition: all 0.3s ease;
}

.neon-input:hover {
  border-color: rgba(100, 181, 246, 0.6) !important;
  box-shadow: 0 0 15px rgba(100, 181, 246, 0.3) !important;
}

.neon-input:focus-within {
  border-color: #64b5f6 !important;
  box-shadow: 
    0 0 20px rgba(100, 181, 246, 0.5),
    inset 0 1px 0 rgba(255, 255, 255, 0.2) !important;
}

:deep(.el-input__wrapper) {
  background: transparent !important;
  box-shadow: none !important;
}

:deep(.el-input__inner) {
  color: white !important;
  font-size: 16px !important;
}

:deep(.el-input__inner::placeholder) {
  color: rgba(255, 255, 255, 0.6) !important;
}

:deep(.el-input__prefix) {
  color: rgba(255, 255, 255, 0.8) !important;
}

/* 霓虹按钮 */
.login-button {
  width: 100%;
  height: 50px;
  font-size: 18px;
  font-weight: 600;
  border-radius: 25px !important;
  background: linear-gradient(45deg, #64b5f6, #42a5f5) !important;
  border: none !important;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 30px rgba(100, 181, 246, 0.4) !important;
}

.login-button:active {
  transform: translateY(0);
}

.button-text {
  position: relative;
  z-index: 2;
}

.button-glow {
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transform: rotate(45deg);
  animation: buttonGlow 3s ease-in-out infinite;
}

@keyframes buttonGlow {
  0% { transform: translateX(-100%) translateY(-100%) rotate(45deg); }
  100% { transform: translateX(100%) translateY(100%) rotate(45deg); }
}

.login-footer {
  text-align: center;
  animation: fadeInUp 1s ease-out 0.6s both;
}

@keyframes fadeInUp {
  0% {
    opacity: 0;
    transform: translateY(30px);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

.demo-account {
  margin: 0;
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
  font-weight: 300;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-box {
    margin: 20px;
    padding: 40px 20px;
  }
  
  .login-title {
    font-size: 28px;
  }
  
  .login-subtitle {
    font-size: 14px;
  }
}

/* 加载动画 */
:deep(.el-loading-mask) {
  background: rgba(0, 0, 0, 0.3) !important;
  backdrop-filter: blur(5px);
}
</style>